<?php
namespace Job\Controller;
use Common\Controller\AdminBase;
class JobController extends AdminBase {
    //招聘模型对象
    private $Job = NULL;
    //应聘模型对象
    private $Candidates = NULL;
    protected function _initialize() {
        parent::_initialize();
        $this->Job = D('Job/Job');
        $this->Candidates = D('Job/Candidates');
    }

    //后台管理首页
    public function index() {
        if (IS_POST) {
            //排序
            $jobid = I('post.jobid');
            $listorder = I('post.listorder');
            if (empty($jobid)) {
                $this->error('请选择需要排序的招聘职位！');
            }
            foreach ($jobid as $k => $id) {
                $this->Job->where(array('jobid' => $id))->save(array('listorder' => $listorder[$k]));
            }
            $this->success('排序更新成功！', U('Job/index'));
            exit;
        }
        //条件
        $where = array();
        $keyword = I('get.keyword');
        if ($keyword) {
            $where['zhiwei'] = array('LIKE', "%{$keyword}%");
            $this->assign('keyword', $keyword);
        }

        $count = $this->Job->where($where)->count();
        $page = $this->page($count, 20);
        $data = $this->Job->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array("listorder" => "DESC", "jobid" => "DESC"))->select();
        //统计
        $online = $this->Job->where(array('status' => 1, 'enddate' => array('GT', time())))->count();
        $candidates = $this->Candidates->count();

        $this->assign('data', $data);
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
        $this->assign('online', $online);
        $this->assign('candidates', $candidates);
        $this->display();
    }

    //添加职位
    public function add() {
        if (IS_POST) {
            $post = $_POST;
            $post['username'] = \Admin\Service\User::getInstance()->username;
            if ($this->Job->addJob($post)) {
                $this->success('添加招聘职位成功！', U('Job/index'));
            } else {
                $error = $this->Job->getError();
                $this->error($error ? $error : '职位发布失败！');
            }
        } else {
            $this->display();
        }
    }

    //编辑职位
    public function edit() {
        if (IS_POST) {
            $post = $_POST;
            if ($this->Job->editJob($post)) {
                $this->success('招聘职位修改成功！', U('Job/index'));
            } else {
                $error = $this->Job->getError();
                $this->error($error ? $error : '招聘职位修改失败！');
            }
        } else {
            $jobId = I('get.jobid', 0, 'intval');
            if (empty($jobId)) {
                $this->error('请选择需要修改的招聘职位！');
            }
            $info = $this->Job->getInfo($jobId);
            if (empty($info)) {
                $this->error('该招聘职位不存在！');
            }
            $this->assign('info', $info);
            $this->display();
        }
    }

    //删除招聘职位
    public function delete() {
        if (IS_POST) {
            $jobId = I('post.jobid');
        } else {
            $jobId = I('get.jobid', 0, 'intval');
        }
        if (empty($jobId)) {
            $this->error('请选择需要删除的招聘职位！');
        }
        if ($this->Job->deleteJob($jobId)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    //应聘简历管理
    public function candidates() {
        //条件
        $where = array();
        $keyword = I('get.keyword');
        if ($keyword) {
            $where['name'] = array('LIKE', "%{$keyword}%");
            $this->assign('keyword', $keyword);
        }
        $jobId = I('get.jobid', 0, 'intval');
        if ($jobId) {
            $where['jobid'] = $jobId;
            $this->assign('jobid', $jobId);
        }

        $count = $this->Candidates->where($where)->count();
        $page = $this->page($count, 20);
        $data = $this->Candidates->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array("id" => "DESC"))->select();
        $data = $data ? $data : array();
        //查询出对应岗位
        foreach ($data as $k => $rs) {
            $data[$k]['jobinfo'] = $this->Job->where(array('jobid' => $rs['jobid']))->find();
        }
        $this->assign('data', $data);
        $this->assign("Page", $page->show('Admin'));
        $this->display();
    }

    //删除应聘简历
    public function deleteyp() {
        if (IS_POST) {
            $id = I('post.ids');
        } else {
            $id = I('get.id', 0, 'intval');
        }
        if (empty($id)) {
            $this->error('请选择需要删除的应聘简历！');
        }
        if ($this->Candidates->deleteCandidates($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

}
